Skip to content

[#28420] Fix type inference for closures over local variables#39210

Open
lakshitbahl wants to merge 1 commit into
apache:masterfrom
lakshitbahl:fix-28420-closure-type-inference
Open

[#28420] Fix type inference for closures over local variables#39210
lakshitbahl wants to merge 1 commit into
apache:masterfrom
lakshitbahl:fix-28420-closure-type-inference

Conversation

@lakshitbahl

@lakshitbahl lakshitbahl commented Jul 3, 2026

Copy link
Copy Markdown

###Description:
This PR resolves closure-related type inference regressions (see #28420) and restores compatibility with Python 3.11+ opcode changes. It introduces two distinct fixes under a unified closure-handling improvement, allowing us to un-skip testLocalClosure.

  1. Type-vs-Value Cell Confusion
    Prevents the inference engine from confusing synthetic inferred types with real runtime closure values. Introduces the _TypeInCell marker class to safely wrap, track, and unwrap inferred types within the frame emulation, ensuring strict isolation from actual Python cell contents.

  2. Python 3.11+ Shared-Slot Indexing
    Fixes LOAD_CLOSURE and LOAD_DEREF indexing for Python 3.11 and newer. Replaces the legacy arg -= len(co_varnames) arithmetic, which broke when captured parameters shared a slot in the new unified localsplus array with version-aware slot-name resolution. This correctly maps variables and prevents out-of-bounds IndexError exceptions that were previously being swallowed by the safe-mode execution and degraded to Any.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@github-actions github-actions Bot added the python label Jul 3, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the summary. You can try again by commenting /gemini summary.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@lakshitbahl lakshitbahl force-pushed the fix-28420-closure-type-inference branch from 0a8d1ab to a831843 Compare July 3, 2026 13:16
@lakshitbahl

Copy link
Copy Markdown
Author

Fixes closure inference on Python ≤3.12 and narrows testLocalClosure's skip from unconditional to 3.13+ only. On 3.13+, MAKE_FUNCTION no longer takes a closure flag (closures attach via SET_FUNCTION_ATTRIBUTE) and the emulation of that path has pre-existing bugs (FunctionType called without a closure for freevar-carrying code; set_function_attribute uses func.code/func.globals/func.name instead of code/globals/name), so emulation-dependent tests are gated to <3.13. Real-closure inference (validated by the un-gated tests) works on all versions including 3.14.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant